<template>
  <div class="reg-container">
    <div class="reg-box">
      <div class="title-box"></div>
      <el-form ref="registerForm" :model="registerForm" :rules="rules">
        <el-form-item prop="username">
          <el-input
            placeholder="请输入用户名"
            v-model="registerForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            type="password"
            v-model="registerForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            placeholder="请确认密码"
            type="password"
            v-model="registerForm.repassword"
          ></el-input>
        </el-form-item>
        <el-button type="primary" class="reg-btn" @click="registerFn"
          >注册</el-button
        >
        <el-link href="" target="info"><router-link to="/login">去登陆</router-link></el-link>
      </el-form>
    </div>
  </div>
</template>

<script>
import { registerAPI } from '@/api/index.js'
export default {
  name: 'my-register',
  data () {
    const samePwdFn = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请确认输入密码'))
      } else if (value !== this.registerForm.password) {
        callback(new Error('密码不一致'))
      }
      callback()
    }
    return {
      registerForm: {
        username: '',
        password: '',
        repassword: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/, message: '输入的密码必须以字母开头', trigger: 'blur' }
        ],
        repassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: samePwdFn, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    registerFn () {
      this.$refs.registerForm.validate(async (valid) => {
        if (valid) {
          // console.log(this.registerForm)
          const { data: res } = await registerAPI(this.registerForm)
          // console.log(res)
          if (res.code !== 0) return this.$message.error(res.message)
          this.$message.success(res.message)
          this.$router.push('/login')
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.reg-container {
  background: url("@/assets/images/login_bg.jpg") center;
  background-size: cover;
  height: 100%;

  .reg-box {
    width: 400px;
    height: 335px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 0 30px;
    box-sizing: border-box;

    .title-box {
      height: 60px;
      background: url("@/assets/images/login_title.png")
        center no-repeat;
    }

    .reg-btn {
      width: 100%;
    }
  }
}
</style>
